const { requestGet } = require('../utils/request');
const config = require('../config');

// 搜索内容
exports.search = async (req, res) => _reslove(req.query, res, 'search');

// 详情
exports.bookDetail = async (req, res) => _reslove(req.query, res, 'bookDetail');

// 文本
exports.bookContentText = async (req, res) => {
	if (req.query?.chapterId) {
		_reslove(req.query, res, 'bookText');
	} else {
		res.json({ code: 404, msg: '章节id错误' });
	}
};

// 分类 目前用的是铅笔网的 但是铅笔网的带宽较慢
exports.getFenlei = async (req, res) => _reslove(req.query, res, 'classify');

//  抽离公共逻辑
async function _reslove(query, res, con) {
	const { source = 'yetian' } = query;
	try {
		// 请求html
		const htmlRes = await requestGet(config[source][con].host(query), source);
		const content = config[source][con].regExp(htmlRes, source);
		res.json({ code: 200, data: content, msg: '操作成功' });
	} catch (error) {
		res.json({ code: 502, msg: '服务端错误，请联系管理员' });
	}
}
